package com.lp;

import com.lp.entity.ListNode;

public class oddEvenList {
    public static void main(String[] args) {
        ListNode ln = new ListNode();
        ListNode ln5 = new ListNode(5,ln);
        ListNode ln4 = new ListNode(4,ln5);
        ListNode ln3 = new ListNode(3,ln4);
        ListNode ln2 = new ListNode(2,ln3);
        ListNode ln1 = new ListNode(1,ln2);
        ListNode head = ln1;
        //遍历链表
        ListNode head1 = head;
        while(head1 != null && head1.next != null){
            System.out.println("原始值："+head1.val);
            head1 = head1.next;
        }
        ListNode listNode = oddEvenListTest(head);
        //遍历链表
        ListNode head2 = listNode;
        while(listNode != null && listNode.next != null){
            System.out.println("处理后值："+listNode.val);
            listNode = listNode.next;
        }
    }

    public static ListNode oddEvenListTest(ListNode head){
        if(head == null){
            return head;
        }
        ListNode evenHead = head.next;
        ListNode odd = head;
        ListNode even = evenHead;
        while(even != null && even.next!= null){
            odd.next = even.next;
            odd = odd.next;
            even.next = odd.next;
            even = even.next;
        }
        odd.next = evenHead;
        return head;
    }
}
